package com.wangbaokang.duanxin;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;

import com.wangbaokang.duanxin.tools.MyConstants;
import com.wangbaokang.duanxin.tools.MyQueryHandler;
import com.wangbaokang.duanxin.tools.Tools;

/**
 * Created by wangbaokang on 2016/8/20 0020.
 */
public class ConversationDetail extends Activity implements OnClickListener {

    /**
     * 联系人的电话号码
     */
    private String address;
    private Context ctx;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ctx = this;

        address = getIntent().getStringExtra("address");
        if(address == null){
            throw new RuntimeException("联系人是空 不知道显示哪个会话记录");
        }

        setContentView(R.layout.activity_conversation_detail);
        init();

        listView = (ListView) findViewById(R.id.lv_conversation_detail);

        adapter = new MyListAdapter(this, null);
        listView.setAdapter(adapter);

        //设置listView之间的分割线为空 不要分割线
        listView.setDivider(null);

        prepareDate();

    }

    private void prepareDate() {

        MyQueryHandler myQueryHandler = new MyQueryHandler(getContentResolver());
        myQueryHandler.setOnCursorChangedListener(new MyQueryHandler.IOnCursorChangedListener() {
            @Override
            /**
             * 当adapter获得cursor的时候回调此方法
             */
            public void onCursorChanged(int token, Object cookie, Cursor cursor) {
                // 让listView显示最后一行
                listView.setSelection(adapter.getCount()-1);
            }
        });
        myQueryHandler.startQuery(99, adapter, MyConstants.URI_SMS, projection, "address="+address, null, " date");

    }

    /**
     * 显示会话详情所需要的列
     */
    private String[] projection = {"_id", "body", "date", "type"};
    /**
     * 短信内容所在索引值为1
     */
    private final int INDEX_BODY = 1;
    /**
     * 短信时间所在索引值为1
     */
    private final int INDEX_DATE = 2;
    /**
     * 短信类型所在索引值为1
     */
    private final int INDEX_TYPE = 3;

    private void init(){

        TextView tittle = (TextView) findViewById(R.id.tv_tittle_conversation_detail);
        String name = Tools.findNameByNumber(ctx, address);
        if (name == null) {
            tittle.setText(address);
        }else {
            tittle.setText(name);
        }

        findViewById(R.id.btn_back).setOnClickListener(this);
        findViewById(R.id.btn_ok).setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:

                finish();
                break;
            case R.id.btn_ok:

                break;

        }
    }

    private MyListAdapter adapter;

    private class MyListAdapter extends CursorAdapter {

        public MyListAdapter(Context context, Cursor c) {
            super(context, c);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            View view = View.inflate(ctx, R.layout.list_item_conversation_detail, null);

            ViewHolder vh = new ViewHolder();
            vh.tlReceive = (TableLayout) view.findViewById(R.id.tl_receive);
            vh.msgReceive = (TextView) view.findViewById(R.id.tv_msg_receive);
            vh.dateReceive = (TextView) view.findViewById(R.id.tv_date_receive);

            vh.tlSend = (TableLayout) view.findViewById(R.id.tl_send);
            vh.msgSend = (TextView) view.findViewById(R.id.tv_msg_send);
            vh.dateSend = (TextView) view.findViewById(R.id.tv_date_send);

            view.setTag(vh);

            return view;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            ViewHolder vh = (ViewHolder) view.getTag();
            //给listView条目这是内容
            int type = cursor.getInt(INDEX_TYPE);//获得短信类型
            String text = cursor.getString(INDEX_BODY);//获得短信内容
            Long when = cursor.getLong(INDEX_DATE);//获得短信日期

            String dateStr = "";
            //使用系统工具类，将日期转换为合适的格式
            if(DateUtils.isToday(when)){
                dateStr = DateFormat.getTimeFormat(context).format(when);
            }else{
                dateStr = DateFormat.getDateFormat(context).format(when);
            }


            if(type == MyConstants.TYPE_RECEIVE ){//接受到的短信
                vh.tlReceive.setVisibility(View.VISIBLE);
                vh.tlSend.setVisibility(View.GONE);

                vh.msgReceive .setText(text);
                vh.dateReceive.setText(dateStr);

            }else {
                vh.tlReceive.setVisibility(View.GONE);
                vh.tlSend.setVisibility(View.VISIBLE);

                vh.msgSend .setText(text);
                vh.dateSend.setText(dateStr);
            }

        }
    }

    private class ViewHolder {

        public TableLayout tlReceive;
        public TextView msgReceive;
        public TextView dateReceive;

        public TableLayout tlSend;
        public TextView msgSend;
        public TextView dateSend;

    }

}
